<template>
  <el-card class="fontAuth">
    <h1>前端权限按钮示例</h1>
    <CurrentPermissionMode />
    <div class="role">当前角色: {{ roleList }}</div>
    <div>
      角色切换(请先切换权限模式为前端角色权限模式):
      <el-button @click="changeRole(RoleEnum.SUPER)" :type="isSuper ? 'primary' : 'default'"
        >super</el-button
      >
      <el-button @click="changeRole(RoleEnum.TEST)" :type="isTest ? 'primary' : 'default'"
        >test</el-button
      >
    </div>
    <el-divider>组件方式判断权限(有需要可以自行全局注册)</el-divider>
    <Authority :value="RoleEnum.SUPER">
      <el-button type="primary"> 拥有super角色权限可见 </el-button>
    </Authority>
    <Authority :value="RoleEnum.TEST">
      <el-button type="success"> 拥有test角色权限可见 </el-button>
    </Authority>
    <Authority :value="[RoleEnum.TEST, RoleEnum.SUPER]">
      <el-button type="danger"> 拥有[test,super]角色权限可见 </el-button>
    </Authority>

    <el-divider>函数方式方式判断权限(适用于函数内部过滤)</el-divider>
    <el-button v-if="hasPermission(RoleEnum.SUPER)" type="primary">
      拥有super角色权限可见
    </el-button>
    <el-button v-if="hasPermission(RoleEnum.TEST)" type="success"> 拥有test角色权限可见 </el-button>
    <el-button v-if="hasPermission([RoleEnum.TEST, RoleEnum.SUPER])" type="danger">
      拥有[test,super]角色权限可见
    </el-button>

    <el-divider>自定义指令方式方式判断权限(该方式不能动态修改权限，不是响应式的)</el-divider>
    <el-button v-auth="RoleEnum.SUPER" type="primary"> 拥有super角色权限可见 </el-button>
    <el-button v-auth="RoleEnum.TEST" type="success"> 拥有test角色权限可见 </el-button>
    <el-button v-auth="[RoleEnum.TEST, RoleEnum.SUPER]" type="danger">
      拥有[test,super]角色权限可见
    </el-button>
  </el-card>
</template>

<script lang="ts">
import { defineComponent, computed } from 'vue'
import CurrentPermissionMode from '../CurrentPermissionMode.vue'
// 使用该方法导入useStore可以加入ts提示
import { useStore } from '@/store'
import { RoleEnum } from '@/enums/roleEnum'
import { usePermission } from '@/hooks/web/usePermission'
import { Authority } from '@/components/Authority/index'

export default defineComponent({
  components: {
    CurrentPermissionMode,
    Authority
  },
  setup() {
    const store = useStore()
    const { hasPermission } = usePermission()

    const roleList = computed(() => store.state.login.roleList)

    function changeRole(role) {
      store.commit('login/setRoleList', [role])
      location.reload()
    }

    return {
      roleList,
      isSuper: computed(() => store.state.login.roleList.includes(RoleEnum.SUPER)),
      isTest: computed(() => store.state.login.roleList.includes(RoleEnum.TEST)),
      changeRole,
      RoleEnum,
      hasPermission
    }
  }
})
</script>

<style lang="scss" scoped>
.fontAuth {
  font-size: 16px;

  h1 {
    font-size: 24px;
    padding: 10px 0;
  }

  .role {
    padding: 10px 0;
  }
}
</style>
